package socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo1 implements Runnable {
	public void run() {
		try {
			@SuppressWarnings("resource")
			ServerSocket server = new ServerSocket(8899);
			System.out.println("服务器启动");
			System.out.println(server.getLocalPort());
			// System.out.println(server.isBound());
			Socket socket = server.accept();
			System.out.println(socket.getPort());
			System.out.println("客户端连接");
			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
			String m;
			do {
				m = (String) in.readObject();
				System.out.println("客户端说:" + m);
			} while (!m.equals("bye"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			System.out.println("在同一个电脑上只能运行一个实例！");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
